package acom.imghandle.aa;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainView {

	private JFrame frame;
	private JPanel pane;
	
	private JTextField str_txt;
	private JTextField size_txt;
	private JButton gen_but;	
	private JLabel img_lb;
	private JButton fenxi_but;
	private JButton print_but;
	private JTextField pirnt_txt;
	private JLabel res_lb;
	
	static final int WIDTH=500;
    static final int HEIGHT=500;
    
    private int position_x=20;
    private int position_y=20;
	
	public MainView(){
		frame = new JFrame();
		frame.setSize(WIDTH,HEIGHT);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pane = new JPanel();
		pane.setLayout(null);
		
		str_txt = new JTextField(15);
		size_txt = new JTextField(5);
		
		gen_but = new JButton ("产生验证码");
		
		img_lb = new JLabel("图片显示");
		
		fenxi_but = new JButton ("分析验证码");
		print_but =  new JButton ("打印");
		
		pirnt_txt = new JTextField(8);
		
		res_lb = new JLabel("结果显示");
		init();
		
		pane.add(str_txt);
		pane.add(size_txt);
		pane.add(gen_but);
		pane.add(img_lb);
		pane.add(fenxi_but);
		pane.add(print_but);
		pane.add(pirnt_txt);
		pane.add(res_lb);
		
		frame.add(pane);
		frame.setVisible(true);
	}
	
	private void init(){
		str_txt.setBounds(position_x,position_y,100,30);
		size_txt.setBounds(position_x+130,position_y,50,30);
		gen_but.setBounds(position_x,position_y+50,150,30);
		img_lb.setBounds(position_x,position_y+100,150,100);
		fenxi_but.setBounds(position_x,position_y+250,80,30);
		print_but.setBounds(position_x+150,position_y+250,80,30);
		pirnt_txt.setBounds(position_x+240,position_y+250,80,30);
		res_lb.setBounds(position_x,position_y+300,280,30);
		
		gen_but.addActionListener(new EventHandler(this,"generate_img_Action"));
		
		print_but.addActionListener(new EventHandler(this,"print_Action"));
		
		fenxi_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
			}
		});
	}
	
	public void print_Action() throws IOException{
		BufferedImage bi = (BufferedImage) ((ImageIcon) img_lb.getIcon()).getImage();

		
		String printcontent = pirnt_txt.getText();
		System.out.println("printcontent==="+printcontent);
		if(printcontent.trim().isEmpty()){
			printcontent ="♦♦";
		}
		
		// 获取图像的宽度和高度
		int width = bi.getWidth();
		int height = bi.getHeight();

		// 扫描图片
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {// 行扫描
				int dip = bi.getRGB(j, i);
				if (dip == -1){
					System.out.print("□□");
				}else{
					System.out.print(printcontent);
				}
			}
			System.out.println();// 换行 ♦♦ ●○
		}
	}
	
	public void generate_img_Action() {
		RandomValidateCode randomValidateCode = new RandomValidateCode();
		
		String size = size_txt.getText();
		
		if(!size.trim().isEmpty()){
			randomValidateCode.setFontSize(Integer.parseInt(size));
		}
			
		
		BufferedImage image =null;
		
		String gen_str = str_txt.getText();
		if(gen_str.trim().isEmpty()){
			image=randomValidateCode.getRandcode();
		}else{
			image=randomValidateCode.getStringcode(gen_str);
		}
		
		img_lb.setText("");
		img_lb.setIcon(new ImageIcon( image) );
	}

	public static void main(String[] args) {
		
		new MainView();
	}

}
